package com.sfh.cpc.note;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaeger.library.StatusBarUtil;
import com.sfh.cpc.R;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.refresh.SwipeRefreshMoreLayout;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.note.model.NoteModel;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.lib.ui.dialog.DialogBuilder;

/**
 * 功能描述: 用户创建话题列表
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/30
 */
public class UserNoteActivity extends AbstractLifecycleActivity<NoteModel> implements SwipeRefreshMoreLayout.OnLoadingListener, BaseQuickAdapter.OnItemClickListener {

    public static void startUI(Context context, long userId) {

        Intent intent = new Intent(context, UserNoteActivity.class);
        intent.putExtra("ID", userId);
        context.startActivity(intent);
    }

    TextView tvTitle;
    WidgetBaseToolbar toolbar;
    SwipeRefreshMoreLayout layout;
    ImageView ivAdd;
    MyNoteAdapter adapter;
    int nowPage;
    long userId;

    UserNoteView noteView;
    int height = 400;
    int overallXScroll = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_my_activity);
        this.toolbar = findViewById(R.id.toolbar);
        StatusBarUtil.setTranslucentForImageView(this, 0, this.toolbar);
        this.tvTitle = this.toolbar.setTitle("");
        this.tvTitle.setTextColor(Color.WHITE);
        this.ivAdd = findViewById(R.id.ivAdd);
        this.layout = findViewById(R.id.swipeRefreshMoreLayout);

        this.userId = getIntent().getLongExtra("ID", 0);

        this.adapter = new MyNoteAdapter();
        this.noteView = new UserNoteView(this);
        this.adapter.addHeaderView(noteView);

        this.layout.setAdapter(adapter, true);
        this.layout.addItemDecoration(R.dimen.px_7);
        this.layout.setOnItemClickListener(this);
        this.layout.setOnLoadingListener(this);
        this.adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                EUser me = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
                final ENote note = (ENote) adapter.getItem(position);

                if (me != null &&  note.getUserId() == me.getUserId()){
                    DialogBuilder builder = new DialogBuilder();
                    builder.setTitle("提示");
                    builder.setMessage("当前帖子删除吗?");
                    builder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogBuilder.DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                            getViewModel().delNote(note.getNoteId());
                        }
                    });
                    showDialog(builder);
                }
                return false;
            }
        });
        layout.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                //dy > 0 时为手指向上滚动,列表滚动显示下面的内容
                //dy < 0 时为手指向下滚动,列表滚动显示上面的内容
                // 累加y值 解决滑动一半y值为0
                overallXScroll = overallXScroll + dy;
                if (overallXScroll <= 0) {
                    //设置标题的背景颜色
                    setalpha(0);

                } else if (overallXScroll > 0 && overallXScroll <= height) {
                    //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) overallXScroll / height;
                    int alpha = (int) (255 * scale);
                    setalpha(alpha);
                } else {
                    setalpha(255);
                }
            }
        });
        this.layout.onAutoRefresh();
    }


    public void setalpha(int alpha) {
        int argb = Color.argb(alpha, 255, 255, 255);
        this.toolbar.setBackgroundColor(argb);
        this.tvTitle.setTextColor(Color.argb(alpha, 0, 0, 0));
        StatusBarUtil.setTranslucentForImageView(this, alpha, this.toolbar);
    }

    @LiveDataMatch
    public void onShowUser(EUser user) {
        this.tvTitle.setText(user.getUserName());
        this.noteView.showData(user);

        EUser me = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
        if (me == null || me.getUserId() != user.getUserId()) {
            this.ivAdd.setVisibility(View.GONE);
        } else {
            this.ivAdd.setVisibility(View.VISIBLE);
        }
    }

    @LiveDataMatch
    @Override
    public void onRefreshUI() {
        this.nowPage = 1;
        this.getViewModel().getUser(userId);
        this.getViewModel().pageNote(nowPage, userId);
    }

    @Override
    public void onLoadMoreUI() {
        this.nowPage++;
        this.getViewModel().pageNote(nowPage, userId);
    }

    @LiveDataMatch
    public void onPageSuccess(RspPage<ENote> data) {

        if (data == null) {
            this.layout.onLoadMoreFail();

        } else {
            this.nowPage = data.getPageNumber();
            this.layout.onRefreshCompale(data.getList(), data.getPageNumber() != 1);
            if (data.getTotalPage() == data.getPageNumber()) {
                this.layout.onLoadAllCompale();
            }
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ENote note = (ENote) adapter.getItem(position);
        NoteActivity.startUI(this, note);
    }

    public void onViewCilcked(View v) {
        NoteAddActivity.startUI(UserNoteActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == RESULT_OK){
            this.onRefreshUI();
        }
    }
}
